What is javascript-natural-sort?
The javascript-natural-sort npm package provides a natural sorting algorithm for JavaScript. It allows you to sort strings in a way that is intuitive to humans, such as sorting 'item2' before 'item10'. This is particularly useful for sorting lists of filenames, version numbers, or any other strings that contain numbers.
What are javascript-natural-sort's main functionalities?
Basic Natural Sort
This feature allows you to sort an array of strings in a natural order. The code sample demonstrates sorting a list of items where the numerical part of the strings is taken into account.
const naturalSort = require('javascript-natural-sort');
const items = ['item10', 'item2', 'item1'];
items.sort(naturalSort);
console.log(items); // Output: ['item1', 'item2', 'item10']
Case Insensitive Sort
This feature allows you to perform a case-insensitive natural sort. The code sample shows how the sorting algorithm treats 'Item10' and 'item10' as equivalent.
const naturalSort = require('javascript-natural-sort');
const items = ['Item10', 'item2', 'item1'];
items.sort(naturalSort);
console.log(items); // Output: ['item1', 'item2', 'Item10']
Locale Aware Sort
This feature allows you to sort strings in a locale-aware manner. The code sample demonstrates sorting a list of strings with special characters.
const naturalSort = require('javascript-natural-sort');
const items = ['ä', 'a', 'z'];
items.sort(naturalSort);
console.log(items); // Output: ['a', 'ä', 'z']
Other packages similar to javascript-natural-sort
natural-sort
The 'natural-sort' package provides a similar natural sorting algorithm. It is lightweight and easy to use, but may not have as many features as 'javascript-natural-sort'.
alphanum-sort
The 'alphanum-sort' package offers alphanumeric sorting capabilities. It is optimized for performance and can handle large datasets efficiently, but may lack some of the locale-aware features of 'javascript-natural-sort'.
string-natural-compare
The 'string-natural-compare' package provides a natural comparison function for strings. It is highly customizable and can be used in various sorting scenarios, but might require more configuration compared to 'javascript-natural-sort'.
Simple numerics
>>> ['10',9,2,'1','4'].sort(naturalSort)
['1',2,'4',9,'10']
Floats
>>> ['10.0401',10.022,10.042,'10.021999'].sort(naturalSort)
['10.021999',10.022,'10.0401',10.042]
Float & decimal notation
>>> ['10.04f','10.039F','10.038d','10.037D'].sort(naturalSort)
['10.037D','10.038d','10.039F','10.04f']
Scientific notation
>>> ['1.528535047e5','1.528535047e7','1.528535047e3'].sort(naturalSort)
['1.528535047e3','1.528535047e5','1.528535047e7']
IP addresses
>>> ['192.168.0.100','192.168.0.1','192.168.1.1'].sort(naturalSort)
['192.168.0.1','192.168.0.100','192.168.1.1']
Filenames
>>> ['car.mov','01alpha.sgi','001alpha.sgi','my.string_41299.tif'].sort(naturalSort)
['001alpha.sgi','01alpha.sgi','car.mov','my.string_41299.tif'
Dates
>>> ['10/12/2008','10/11/2008','10/11/2007','10/12/2007'].sort(naturalSort)
['10/11/2007', '10/12/2007', '10/11/2008', '10/12/2008']
Money
>>> ['$10002.00','$10001.02','$10001.01'].sort(naturalSort)
['$10001.01','$10001.02','$10002.00']
Movie Titles
>>> ['1 Title - The Big Lebowski','1 Title - Gattaca','1 Title - Last Picture Show'].sort(naturalSort)
['1 Title - Gattaca','1 Title - Last Picture Show','1 Title - The Big Lebowski']
By default - case-sensitive sorting
>>> ['a', 'B'].sort(naturalSort);
['B', 'a']
To enable case-insensitive sorting
>>> naturalSort.insensitive = true;
>>> ['a', 'B'].sort(naturalSort);
['a', 'B']